<?php


namespace app\home\controller;

use think\Db;
use think\facade\Config;
use app\common\library\Qiniu;
use think\facade\Request;
class News extends Base
{
    protected $beforeActionList = [
        'first',
    ];

    protected function first()
    {
        $this->assign('nav', 'news');
    }

    public function index()
    {
        $count = DB::name('news')->count();
        if ($this->request->isPost()) {
            $map = array();
            $data = array();
            $map[]=['category_id','in','1,2,3'];
            $data['count'] = $count;
            $data['data'] = Db::name('news')
                ->where($map)
                // ->fetchSql(true)
                ->order('id desc')->page(input("page"), 20)->select();
            $site_category = config('param.site_category');
            foreach ($data['data'] as &$info) {
                $info['category_id']='['.$site_category[$info['category_id']].']';
                $info['c_time'] = date('Y-m-d', $info['c_time']);
            }
            if ($data['count'] > 0 && $data['data']) {
                $data['msg'] = '获取成功';
            } else {
                $data['msg'] = '获取数据为空';
            }
            $data['code'] = 0;
            return json($data);
        } else {

            $this->assign('count', $count);
            return view();
        }

    }

    public function info()
    {
        $info = DB::name('news')->where('id', input('id'))->find();
        if ($info) {
            $site_category = config('param.site_category');
            $info['category_id']=$site_category[$info['category_id']];
            $this->assign('info', $info);
            return view();
        } else {
            $this->redirect('home/index/index');
        }

    }


}